﻿/*
 *  Source code courtesy of the desktopWeb.CodePlex.com community project. See MS-PL license on Codeplex.com. 
 *  This is beta code not intended for a production application.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing.Design;
using System.Data.SqlClient;
using System.Collections.Specialized;
using System.Configuration;

namespace MyData.Common
{
  public class WorkflowConfiguration
  {
    private string _defaultWorkflow = string.Empty;
    [Category("Office Workflow"), DisplayName("Default Workflow"),
    Editor(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(UITypeEditor))]
    public string DefaultWorkflow
    {
      get
      {
        return this.GetAppSetting("DefaultWorkflow");
      }
      set
      {
        this.CreateAppSetting("DefaultWorkflow", value);
        _defaultWorkflow = this.GetAppSetting("DefaultWorkflow"); ;
      }
    }

    private string _sampleDocument = string.Empty;
    [Category("Office Workflow"), DisplayName("Sample Document"),
    Editor(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(UITypeEditor))]
    public string SampleDocument
    {
      get
      {
        return this.GetAppSetting("SampleDocument");
      }
      set
      {
        this.CreateAppSetting("SampleDocument", value);
        _sampleDocument = this.GetAppSetting("SampleDocument"); ;
      }
    }
    
    private string _sharePointUrl = string.Empty;
    [Category("SharePoint"), Description("SharePoint Url for connecting to service.")]
    public string SharePointUrl
    {
      get
      {
        return this.GetAppSetting("SharePointUrl");
      }
      set
      {
        this.CreateAppSetting("SharePointUrl", value);
        _sharePointUrl = this.GetAppSetting("SharePointUrl"); ;
      }
    }

    public string GetAppSetting(string name)
    {
      string value = null;
      try
      {
        // Get the AppSettings section.
        value = ConfigurationManager.AppSettings[name];
      }
      catch (ConfigurationErrorsException e)
      {
        //Handle in a production application
      }

      return value;
    }

    public void CreateAppSetting(string name, string value)
    {
      // Get the application configuration file.
      System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

      //Do not append setting values
      config.AppSettings.Settings.Remove(name);
      config.AppSettings.Settings.Add(name, value);

      // Save the configuration file.
      config.Save(ConfigurationSaveMode.Modified);

      ConfigurationManager.RefreshSection("appSettings");
    }
  }
}
